<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of qrcodeModel
 * 二维码处理
 * @author xlp
 */
class qrcodeModel extends model {

    function __construct() {
        parent::__construct();
        $this->dbTable = 'qrcode';
    }

    /*
     * 根据id获取二维码信息
     * @param $id int id
     * @return array
     */

    public function getQrcodeById($id) {
        $rs = $this->where(array('scene_id' => $id))->find();
        if (!$rs) {
            return false;
        }
        if ($rs['info']) {
            $rs['info'] = json_decode($rs['info'], true);
        }
        return $rs;
    }

    /*
     * 根据uid和类型获取二维码信息
     * @param $uid int uid
     * @param $mod int 二维码场景 0:邀请 ....
     * @param $type int 类型 0:临时 1:永久 默认:0
     * @return array
     */

    public function getQrcodeByUid($uid, $mod = 0, $type = 0) {
        $rs = $this->where(array('uid' => $uid, 'qmod' => $mod, 'stype' => $type))->find();
        if (!$rs) {
            return false;
        }
        if ($rs['info']) {
            $rs['info'] = json_decode($rs['info'], true);
        }
        return $rs;
    }

    /*
     * 根据类型和过期时间生成二维码信息
     * @param $uid int uid
     * @param $mod int 二维码场景 0:邀请 1:网站二维码 2:固定小区 3:商家推广 4:扫码领小管家优惠券....
     * @param $type int 类型 0:临时 1:永久 默认:0
     * @param $expireTime int 有效时间 默认:1800
     * @return array
     */

    public function setQrcode($uid, $mod = 0, $type = 0, $expireTime = 1800) {
        if ($type) {
            $type = 1;
            $expireTime = 0;
        } else {
            $expireTime = $expireTime + TIME;
        }
        return $this->insert(array('uid' => $uid, 'qmod' => $mod, 'stype' => $type, 'info' => '', 'expire_time' => $expireTime));
    }

    /*
     * 更新二维码信息
     * @param $id int id
     * @param $info array 信息数组
     * @param $expireTime int 有效时间 默认:1800
     * @return array
     */

    public function setQrcodeInfo($id, $info = array(), $expireTime = 0) {
        $objData = array(
            'info' => $info ? json_encode($info) : ''
        );
        if ($expireTime) {
            $objData['expire_time'] = $expireTime + TIME;
        } else {
            $objData['expire_time'] = 0;
        }
        return $this->update($objData, array('scene_id' => $id));
    }

}
